package pacBioSimple;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import pacBiopackage.GetOpt;
import tools.kmer.KmerSet_binary;
import tools.kmer.kmerFunctions;

public class PacBioSeedEntropyMain {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		GetOpt go = new GetOpt(args, "o:k:a:h:n:l:j");
		go.optErr = true;
		int ch = -1;
		
		// process options in command line arguments
		// Set default values
		boolean usagePrint = false;                 
		String filenameAlignment = "";   
		String filenameOutput="output.txt";
		String filenameKmerObject = "";
		boolean jelly=false;
		int kmer_count_limit =100000;
		int left_limit=42;
		while ((ch = go.getopt()) != GetOpt.optEOF) 
		{
		    if ((char)ch == 'h') {
		       usagePrint = true;
		   }
		    else if ((char)ch == 'a') {
		    	filenameAlignment = go.optArgGet();
		    }
		    else if((char)ch =='o')
		    {
		    	filenameOutput = go.optArgGet();
		    }
		    else if((char)ch =='k')
		    {
		    	filenameKmerObject=go.optArgGet();
		    }
		    else if((char)ch =='n')
		    {
		    	kmer_count_limit=Integer.valueOf(go.optArgGet());
		    }
		    else if((char)ch =='j')
		    {
		    	jelly=true;
		    }
		    else if((char)ch =='l')
		    {
		    	left_limit=Integer.valueOf(go.optArgGet());
		    }
		    else
		        System.exit(1);                     // undefined option
		}                                           // getopt() returns '?'
		
		if (usagePrint || filenameKmerObject.isEmpty() || filenameAlignment.isEmpty()) {
		    System.out.println("Usage: -a PacBio -k Kmer -n kmer_count_limit -l left_limit -j[ -o outputFile -h]"); //NOI18N
		    System.exit(0);
		}
		
		// @TODO check if files exist
	
		System.out.println("------------------------");
		System.out.println("Command line parameter settings:");
		System.out.println("-a " + filenameAlignment); //NOI18N
		System.out.println("-o " + filenameOutput); //NOI18N
		System.out.println("-n " + kmer_count_limit); //NOI18N
		System.out.println("-l " + left_limit); //NOI18N
		System.out.println("-k " + filenameKmerObject); //NOI18N
		System.out.println("-j " + jelly); //NOI18N
		System.out.println("------------------------");
		
		
		//first we have to read the kmersFromthe file
		KmerSet_binary ks = null;
		MyUtil.readKmers(filenameKmerObject, jelly, left_limit, kmer_count_limit, ks);
		BufferedReader br = null;
		try
		{
			//kmer_count_limit=170;
			br = new BufferedReader(new FileReader(filenameAlignment));
			PrintWriter containseeds = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_contain_seeds"))); 
			PrintWriter containseeds_not = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_not_contain_seeds")));
			PrintWriter pw2 =new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_single")));
			PrintWriter pw3 = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_entropy_seeds")));
			PrintWriter pw4 = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_seed_numbers")));
			PrintWriter pw5 = new PrintWriter(new BufferedWriter(new FileWriter(filenameOutput+"_seed_entropy_max_per_read")));
			//PacBioErrorStatisticsContainer stat = new PacBioErrorStatisticsContainer();
			CodingTable table = new CodingTable();
			PacBioMaster correctionKmer = new PacBioMaster(ks, table,kmer_count_limit, null,0.0);
			//PacBioCorrectionByKmers kmerCorrection = new PacBioCorrectionByKmers(ks,stat,freq_mode,freq_table);
			System.out.println("starting correction");
			List<Character> alphabet = new ArrayList<Character>();
			
			alphabet.add('A');
			alphabet.add('C');
			alphabet.add('G');
			alphabet.add('T');
			
			Entropy<Character> entropy = new Entropy<Character>(alphabet);
			while(br.ready())
			{
				//test if we are getting a copy of br or if its counting up
				PacBioReadContainer read = new PacBioReadIntoContainer().readIntoContainer(br);
				List<Double> result=correctionKmer.seedEntropy(read, entropy);
				double sum=0.0;
				double max=0.0;
				for(int i=0; i < result.size();++i)
				{
					sum+=result.get(i);
					if(max<result.get(i))
					{
						max=result.get(i);
					}
					pw3.append(String.valueOf(result.get(i))+"\n");
				}
				if(sum!=0.0)
				{
					pw5.append(max+" "+sum+" "+(max/sum)+" "+read.getHeader()+"\n");
				}
				else
				{
					pw5.append(max+" "+sum+" "+sum+" "+read.getHeader()+"\n");
				}
				
				pw4.append(String.valueOf(result.size()));
				pw4.append("\n");
				List<Integer>seedsResult = correctionKmer.getSeeds();
				if(seedsResult.size()==0)
				{
					containseeds_not.append(read.getHeader()+"\n");
				}
				else
				{
					containseeds.append(read.getHeader()+"\n");
				}
				for(int i=0; i < seedsResult.size();++i)
				{
					pw2.append(String.valueOf(seedsResult.get(i))+" ");
				}
				pw2.append("\n");
				
			}
			pw4.flush();
			pw2.flush();
			pw3.flush();
			pw2.close();
			pw3.close();
			pw4.close();
			pw5.flush();
			pw5.close();
			containseeds.flush();
			containseeds_not.flush();
			containseeds.close();
			containseeds_not.close();
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}
